<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\CommonController;
use App\Models\shop\Brand;
use DB;

class BrandController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_system' => 'shop',
            '_brand' => 'am-active',
        ]);
    }
        //李文涛写法
 /*   public function index(Request $request)
    {
        $where=function ($query) use ($request)
        {
            if($request->has('keyword') and $request->keyword != '')
            {
                $search="%".$request->keyword."%";
                $query->where('name','like',$search);
            }
        };
        $brands=Brand::where($where)->OrderBy('sort_order')->paginate(4);
    }*/


    //重写index  把品牌信息显示在页面
    public function index(Request $request)
    {
        $keyword=$request->keyword;
        $brands=DB::table('brands')->where('name','like', "%$keyword%")->paginate(config('fangshop.page_size'));
        return view('admin.shop.brands.index')->with('brands',$brands);
    }


   /* //搜索
    function search(Request $request)
    {

        $keyword=$request->keyword;
        $brands=DB::select("select * from brands where name like '%$keyword%' ");
        return view('fangshop.admin.brand')->with('brands',$brands);
    }*/



    //显示添加
    public function create()
    {
        return view('admin.shop.brands.create');
    }
    //执行添加
    public function store(Request $request)
    {
        //        return $request->all();                  //相当于dump($_POST)
//        $article = new Article;
//        $article->title = $request->title;
//        $article->content = $request->input('content');
//        $article->save();
//        return $request->all();
        Brand::create($request->all());//把所有提交上来的数据添加到数据库
        return redirect(route('shop.brand.index'))->with('brand','新增品牌成功~');
    }

    //编辑brand
    public function edit($id)
    {
        $brand=Brand::find($id);
        return view('admin.shop.brands.edit')->with('brand',$brand);
    }

    //更新brand
    public function update(Request $request,$id)
    {
        $brand=Brand::find($id);
        $brand->update($request->all()); //修改request上提交的所有数据
        return redirect(route('shop.brand.index'))->with('brand','编辑成功！');
    }

    //删除
    function destroy($id)
    {
        Brand::destroy($id);
        return redirect(route('shop.brand.index'))->with('brand','删除品牌成功~');
    }



    //排序
    function sort_order(Request $request)
    {
//        return $request->all();
        $brand = Brand::find($request->id);
        $brand->sort_order = $request->sort_order;
        $brand->save();
    }


    //是否显示该品牌
    public function is_show(Request $request)
    {
        $brand=Brand::find($request->id);
        $brand->is_show=$brand->is_show?false:true;
//        $brand->is_show=!$brand->is_show;
        $brand->save();
    }


}
